package com.zoonref.viewbehavior;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.appbar.AppBarLayout;

public class SampleTitleBehaviorHead extends CoordinatorLayout.Behavior<View> {
    // 列表顶部和title底部重合时，列表的滑动距离。
    private float deltaY;
    private float startY;//开始变得地方


    public SampleTitleBehaviorHead() {
    }

    public SampleTitleBehaviorHead(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return dependency instanceof AppBarLayout;
    }

    //被观察的view发生改变时回调
    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        ViewGroup appBarLayout = (ViewGroup) parent.getChildAt(0);
        ViewGroup collapsingToolbarLayout = (ViewGroup) appBarLayout.getChildAt(0);

        ViewGroup linearLayout = (ViewGroup) collapsingToolbarLayout.getChildAt(0);
        int height = collapsingToolbarLayout.getHeight();

        View childAt = linearLayout.getChildAt(0);
        int[] screenLocation = new int[2];
        childAt.getLocationOnScreen(screenLocation);
        startY = child.getHeight();
        Log.e("dddd", "startY:" + startY + "screenLocation[1]" + screenLocation[1]);
        if (screenLocation[1] < -10) {
            if ((( - screenLocation[1]+10.0f) / height) > 1) {
                child.setAlpha(1f);

            } else {
                child.setAlpha(( - screenLocation[1]+10.0f) / height);
               // child.setAlpha(0.1f);

            }
            child.setVisibility(View.VISIBLE);

            return true;
        }
        child.setVisibility(View.GONE);
        child.setAlpha(0);




        return true;
    }
}
